<?php
namespace User\Controller;
use Think\Page;
use User\Model\FactoryModel;
use User\Model\LpicModel;
use User\Model\MeetingModel;

class LpicController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function index()
    {
        $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
        $cond['token'] = $this->wxuser['token'];

        $count = M("live_pic")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("live_pic")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function lpicAdd()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('lpicUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '活动名称']);
        }

        $data['name'] = $params['name'];
        if (!empty($params['start_date'])) {
            $data['start_date'] = $params['start_date'];
        }
        if (!empty($params['end_date'])) {
            $data['end_date'] = $params['end_date'];
        }
        if (!empty($params['banner_img'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['banner_img'], 'lpic');
            if ($result['code'] == 1) {
                $data['banner_img'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
            }
        }
        if (!empty($params['head_img'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['head_img'], 'lpic');
            if ($result['code'] == 1) {
                $data['head_img'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
            }
        }
        $data['token'] = $this->wxuser['token'];
        $data['has_head'] = $params['has_head'];
        $data['describe'] = $params['describe'];
        $data['show_describe'] = $params['show_describe'];
        $data['pic_check'] = $params['pic_check'];
        $data['pic_order'] = $params['pic_order'];
        $data['color'] = $params['color'];
        $data['t_order_type'] = $params['t_order_type'];
        $data['show_detail'] = ($params['show_name'] === "on" ? 1 : 0) . ($params['show_nickname'] === "on" ? 1 : 0)
            . ($params['show_time'] === "on" ? 1 : 0) . ($params['show_desc'] === "on" ? 1 : 0);
        $data['status'] = LpicModel::STATUS_PENDING;
        M("live_pic")->add($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function lpicUpdate()
    {
        $type = I("get.type", "", "");
        $id = I("get.id", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $lpic = M("live_pic")->where(['id' => $id])->find();
            $this->assign("lpic", $lpic);
            return $this->display();
        }
        $id = I("post.id", "", "");
        if (empty($id)) {
            $this->ajaxReturn(['code' => -1, 'msg' => '无活动信息']);
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '活动名称']);
        }

        $data['name'] = $params['name'];
        if (!empty($params['start_date'])) {
            $data['start_date'] = $params['start_date'];
        }
        if (!empty($params['end_date'])) {
            $data['end_date'] = $params['end_date'];
        }
        if (!empty($params['banner_img'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['banner_img'], 'lpic');
            if ($result['code'] == 1) {
                $data['banner_img'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
            }
        }
        if (!empty($params['head_img'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['head_img'], 'lpic');
            if ($result['code'] == 1) {
                $data['head_img'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
            }
        }
        $data['token'] = $this->wxuser['token'];
        $data['has_head'] = $params['has_head'];
        $data['describe'] = $params['describe'];
        $data['show_describe'] = $params['show_describe'];
        $data['pic_check'] = $params['pic_check'];
        $data['pic_order'] = $params['pic_order'];
        $data['color'] = $params['color'];
        $data['t_order_type'] = $params['t_order_type'];
        $data['show_detail'] = ($params['show_name'] === "on" ? 1 : 0) . ($params['show_nickname'] === "on" ? 1 : 0)
            . ($params['show_time'] === "on" ? 1 : 0) . ($params['show_desc'] === "on" ? 1 : 0);
        M("live_pic")->where(['id' => $id])->save($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function lpicActive()
    {
        $id = I("get.id", "", "");
        $data['status'] = LpicModel::STATUS_ACTIVE;
        if (!empty($id)) {
            M("live_pic")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function lpicDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($id)) {
            M("live_pic")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function lpicPend()
    {
        $id = I("get.id", "", "");
        $data['status'] = LpicModel::STATUS_PENDING;
        if (!empty($id)) {
            M("live_pic")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function share()
    {
        $params = I('post.', '', '');
        if (empty($params)) {
            $id = I('get.id', '', '');
            if (empty($id)) {
                return $this->redirect('/User/Lpic/index');
            }
            $lpic = M("live_pic")->where(['id' => $id])->find();
            if (empty($lpic)) {
                return $this->redirect('/User/Lpic/index');
            }
            $this->assign('lpic', $lpic);
            return $this->display();
        } else {
            if (!empty($params['id'])) {
                $data_info['share_title'] = $params['share_title'];
                $data_info['share_desc'] = $params['share_desc'];
                $data_info['share_url'] = $params['share_url'];
                $img = $params['share_pic'];
                if (!empty($img)) {
                    $ajaxImg = FactoryModel::ajaxImgUploadThumb($img, "lpic");
                    if ($ajaxImg['code'] != 1) {
                        return $this->my_error('图片上传失败');
                    }
                    $data_info['share_pic'] = "http://" . $_SERVER['HTTP_HOST'] . $ajaxImg['msg'];
                }
                M("live_pic")->where(['id' => $params['id']])->save($data_info);
            }
            return $this->my_success("成功");
        }
    }

    public function menu()
    {
        $cond['live_id'] = I('get.id', -1, '');
        $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
        $list = M("live_pic_menu")->where($cond)->order('priority asc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function menuUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $menuId = I("get.menuId", '', '');
            $liveId = I("get.liveId", '', '');
            if (empty($menuId)) {
                $type = 'add';
            } else {
                $menu = M("live_pic_menu")->where("menu_id=$menuId")->find();
                $this->assign("menu", $menu);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('menuUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写菜单名称']);
        }
        $data['name'] = $params['name'];
        $data_test = '';
        if (!empty($params['icon'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['icon'], 'lpic');
            if ($result['code'] == 1) {
                $data['icon'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
                list($width, $height, $type, $attr) = getimagesize($data['icon']);
                $data['icon'] = $data['icon'] . "?w=" . $width . "&h=" . $height;
            }
        }
        $data['content'] = $params['content'];
        $data['live_id'] = $cond['live_id'] = $params['live_id'];
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $max_priority = M("live_pic_menu")->where($cond)->max('priority');
        $data['priority'] = $max_priority + 1;
        if (empty($params['menuId'])) {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            M("live_pic_menu")->add($data);
        } else {
            $cond_menu['menu_id'] = $params['menuId'];
            unset($data['priority']);
            M("live_pic_menu")->where($cond_menu)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功', 'live_id' => $data['live_id'], 'test' => $data_test]);
    }

    public function menuDelete()
    {
        $menuId = I("get.menuId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($menuId)) {
            M("live_pic_menu")->where(['menu_id' => $menuId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function menuPriorityInc()
    {
        $cond_menu['menu_id'] = $menuId = I("get.menuId", -1, "");
        $liveId = I("get.liveId", -1, "");
        $priority = M("live_pic_menu")->where($cond_menu)->getField('priority');
        $cond['live_id'] = $liveId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $max_priority = M("live_pic_menu")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['live_id'] = $liveId;
            $cond_change['priority'] = $priority + 1;
            $data_change['priority'] = $priority;
            M("live_pic_menu")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("live_pic_menu")->where(['menu_id' => $menuId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }

    }

    public function menuPriorityDec()
    {
        $cond_menu['menu_id'] = $menuId = I("get.menuId", -1, "");
        $liveId = I("get.liveId", -1, "");
        $priority = M("live_pic_menu")->where($cond_menu)->getField('priority');;
        $cond['live_id'] = $liveId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $min_priority = M("live_pic_menu")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['live_id'] = $liveId;
            $cond_change['priority'] = $priority - 1;
            $data_change['priority'] = $priority;
            M("live_pic_menu")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("live_pic_menu")->where(['menu_id' => $menuId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function register()
    {
        $cond['live_id'] = I('get.id', -1, '');
        $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);

        //--会议信息--
        $meet = M("meeting")->where($cond)->find();
        $this->assign('meet', $meet);

        $query_value1 = I('get.query_value1', '', '');
        if ($query_value1 !== '') {
            $cond['query_value1'] = $query_value1;
        }

        $status = I('get.status', -1, '');
        if ($status != -1) {
            $cond['status'] = $status;
        }
        $count = M("live_pic_user")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("live_pic_user")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('user_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function registerUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $userId = I("get.userId", '', '');
            $liveId = I("get.liveId", '', '');
            if (empty($userId)) {
                $type = 'add';
            } else {
                $user = M("live_pic_user")->where("user_id=$userId")->find();
                $this->assign("user", $user);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('registerUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写用户名称']);
        }

        $data['name'] = $params['name'];
        $data['company'] = $params['company'];
        $data['nickname'] = $params['nickname'];
        $data['phone'] = $params['phone'];
        $data['status'] = $params['status'];
        $data['result'] = $params['result'];
        $data['live_id'] = $params['live_id'];
        if (empty($params['userId'])) {
            $cond['phone'] = $params['phone'];
            $cond['live_id'] = $params['live_id'];
            $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
            $count = M("live_pic_user")->where($cond)->count();
            if ($count) {
                $this->ajaxReturn(['code' => -2, 'msg' => '此手机号码已经被注册']);
            }
            M("live_pic_user")->add($data);
        } else {
            $cond['phone'] = $params['phone'];
            $cond['live_id'] = $params['live_id'];
            $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
            $user = M("live_pic_user")->where($cond)->find();
            if ($user['user_id'] != $params['userId']) {
                $this->ajaxReturn(['code' => -2, 'msg' => '此手机号码已经被注册' . json_encode($user)]);
            }
            $cond_user['user_id'] = $params['userId'];
            M("live_pic_user")->where($cond_user)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功', 'live_id' => $data['live_id']]);
    }

    public function registerActive()
    {
        $userId = I("get.userId", "", "");
        $data['status'] = LpicModel::STATUS_ACTIVE;
        if (!empty($userId)) {
            M("live_pic_user")->where(['user_id' => $userId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function registerPend()
    {
        $userId = I("get.userId", "", "");
        $data['status'] = LpicModel::STATUS_PENDING;
        if (!empty($userId)) {
            M("live_pic_user")->where(['user_id' => $userId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function registerReject()
    {
        $userId = I("get.userId", "", "");
        $data['status'] = LpicModel::STATUS_REJECT;
        if (!empty($userId)) {
            M("live_pic_user")->where(['user_id' => $userId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function registerDelete()
    {
        $userId = I("get.userId", "", "");
        $data['status'] = LpicModel::STATUS_DELETE;
        if (!empty($userId)) {
            M("live_pic_user")->where(['user_id' => $userId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function item()
    {
        $cond['live_id'] = I('get.id', -1, '');
        $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
        $list = M("live_pic_item")->where($cond)->order('priority asc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function itemUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $itemId = I("get.itemId", '', '');
            $liveId = I("get.liveId", '', '');
            if (empty($itemId)) {
                $type = 'add';
            } else {
                $item = M("live_pic_item")->where("item_id=$itemId")->find();
                $this->assign("item", $item);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('itemUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写专题名称']);
        }
        $data['name'] = $params['name'];
        $data['describe'] = $params['describe'];
        $data['type'] = $params['type'];
        $data['live_id'] = $cond['live_id'] = $params['live_id'];
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $max_priority = M("live_pic_item")->where($cond)->max('priority');
        $data['priority'] = $max_priority + 1;
        if (empty($params['itemId'])) {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            M("live_pic_item")->add($data);
        } else {
            $cond_menu['item_id'] = $params['itemId'];
            unset($data['priority']);
            M("live_pic_item")->where($cond_menu)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功', 'live_id' => $data['live_id']]);
    }

    public function itemDelete()
    {
        $itemId = I("get.itemId", "", "");
        $data['status'] = LpicModel::STATUS_DELETE;
        if (!empty($itemId)) {
            M("live_pic_item")->where(['item_id' => $itemId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function itemPriorityInc()
    {
        $cond_item['item_id'] = $itemId = I("get.itemId", -1, "");
        $liveId = I("get.liveId", -1, "");
        $priority = M("live_pic_item")->where($cond_item)->getField('priority');
        $cond['live_id'] = $liveId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $max_priority = M("live_pic_item")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['live_id'] = $liveId;
            $cond_change['priority'] = $priority + 1;
            $data_change['priority'] = $priority;
            M("live_pic_item")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("live_pic_item")->where(['item_id' => $itemId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }

    }

    public function itemPriorityDec()
    {
        $cond_item['item_id'] = $itemId = I("get.itemId", -1, "");
        $liveId = I("get.liveId", -1, "");
        $priority = M("live_pic_item")->where($cond_item)->getField('priority');;
        $cond['live_id'] = $liveId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $min_priority = M("live_pic_item")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['live_id'] = $liveId;
            $cond_change['priority'] = $priority - 1;
            $data_change['priority'] = $priority;
            M("live_pic_item")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("live_pic_item")->where(['item_id' => $itemId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function itemPicUpdate()
    {
        $cond_item['item_id'] = I("get.itemId", -1, "");
        $cond_item['status'] = LpicModel::STATUS_ACTIVE;
        $list = M("live_pic_pic")->where($cond_item)->order('priority asc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function itemPicRemove()
    {
        $cond['pic_id'] = I("get.picId", "", "");
        $data['item_id'] = 0;
        $data['priority'] = 0;
        M("live_pic_pic")->where($cond)->save($data);
        return $this->my_success("成功");
    }

    public function itemPicPriorityInc()
    {
        $cond_item['pic_id'] = $picId = I("get.picId", -1, "");
        $priority = M("live_pic_pic")->where($cond_item)->getField('priority');
        $itemId = I("get.itemId", "", "");
        $cond['item_id'] = $itemId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $max_priority = M("live_pic_pic")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['item_id'] = $itemId;
            $cond_change['priority'] = $priority + 1;
            $data_change['priority'] = $priority;
            M("live_pic_pic")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("live_pic_pic")->where(['pic_id' => $picId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function itemPicPriorityDec()
    {
        $cond_item['pic_id'] = $picId = I("get.picId", -1, "");
        $priority = M("live_pic_pic")->where($cond_item)->getField('priority');
        $itemId = I("get.itemId", "", "");
        $cond['item_id'] = $itemId;
        $cond['status'] = LpicModel::STATUS_ACTIVE;
        $min_priority = M("live_pic_pic")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['status'] = LpicModel::STATUS_ACTIVE;
            $cond_change['item_id'] = $itemId;
            $cond_change['priority'] = $priority - 1;
            $data_change['priority'] = $priority;
            M("live_pic_pic")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("live_pic_pic")->where(['pic_id' => $picId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function pic()
    {
        $cond['live_id'] = I('get.id', -1, '');
        $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
        $start_date = urldecode(I("get.start_date", "", ""));
        $end_date = urldecode(I("get.end_date", "", ""));
        $item_id = urldecode(I("get.item_id", "", ""));
        $author = urldecode(I("get.author", "", ""));
        $nickname = urldecode(I("get.nickname", "", ""));
        if ($item_id != "-1" && $item_id != '') {
            $cond['item_id'] = $item_id;
        }
        if (!empty($start_date) && !empty($end_date)) {
            $cond['added_date'] = array("between", array($start_date, $end_date));
        } elseif (!empty($start_date)) {
            $cond['added_date'] = array("GT", $start_date);
        } elseif (!empty($end_date)) {
            $cond['added_date'] = array("LT", $end_date);
        }
        if (!empty($author)) {
            $cond['author'] = $author;
        }
        if (!empty($nickname)) {
            $cond['nickname'] = $nickname;
        }
        $count = M("live_pic_pic")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("live_pic_pic")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('added_date desc')->select();
        $this->assign("list", $list);

        $cond_pic_item['live_id'] = I('get.id', -1, '');
        $cond_pic_item['status'] = array("NEQ", LpicModel::STATUS_DELETE);
        $pic_item_list = M("live_pic_item")->where($cond_pic_item)->getField("item_id,name");
        $this->assign("pic_item_list", $pic_item_list);


        $this->display();
    }

    public function picUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $picId = I("get.picId", '', '');
            $liveId = I("get.liveId", '', '');
            if (empty($picId)) {
                $type = 'add';
            } else {
                $pic = M("live_pic_pic")->where("pic_id=$picId")->find();
                $this->assign("pic", $pic);
                $type = 'update';
            }
            $this->assign('type', $type);
            $cond['live_id'] = $liveId;
            $cond['status'] = array("NEQ", LpicModel::STATUS_DELETE);
            $item_list = M("live_pic_item")->where($cond)->select();
            $this->assign('item_list', $item_list);
            return $this->display('picUpdate');
        }
        $data['describe'] = $params['describe'];
        if (!empty($params['pic_url'])) {
            $result = FactoryModel::ajaxImgUpload($params['pic_url'], 'lpic');
            if ($result['code'] == 1) {
                $data['pic_url'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
                list($width, $height, $type, $attr) = getimagesize($data['pic_url']);
                $data['pic_url'] .= "?w=" . $width . "&h=" . $height;
            }
            $result = FactoryModel::ajaxImgUploadThumb($params['pic_url'], 'lpic');
            if ($result['code'] == 1) {
                $data['pic_url_mid'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
                list($width, $height, $type, $attr) = getimagesize($data['pic_url_mid']);
                $data['pic_url_mid'] .= "?w=" . $width . "&h=" . $height;
            }
            $result = FactoryModel::ajaxImgUploadThumb($params['pic_url'], 'lpic', 200);
            if ($result['code'] == 1) {
                $data['pic_url_min'] = "http://" . $_SERVER['HTTP_HOST'] . $result['msg'];
                list($width, $height, $type, $attr) = getimagesize($data['pic_url_min']);
                $data['pic_url_min'] .= "?w=" . $width . "&h=" . $height;
            }
        }
        $data['author'] = $params['author'];
        $data['nickname'] = $params['nickname'];
        $data['live_id'] = $params['live_id'];
        $data['status'] = $params['status'];
        $data['item_id'] = $params['item_id'];
        $data['result'] = $params['result'];
        if (empty($params['picId'])) {
            if (($data['item_id'] != 0) && ($data['status'] == LpicModel::STATUS_ACTIVE)) {
                $cond_item['item_id'] = $data['item_id'];
                $max_priority = M("live_pic_pic")->where($cond_item)->max('priority');
                $data['priority'] = $max_priority + 1;
            } else {
                $data['priority'] = 0;
            }
            $data['day'] = date("Y-m-d");
            $data['hour'] = date("Y-m-d H");
            M("live_pic_pic")->add($data);
        } else {
            $cond_pic['pic_id'] = $params['picId'];
            if (($data['item_id'] != 0) && ($data['status'] == LpicModel::STATUS_ACTIVE)) {
                $self_pic = M("live_pic_pic")->where($cond_pic)->find();
                $item_id = $self_pic['item_id'];
                $status = $self_pic['status'];
                if (($data['item_id'] == $item_id) && ($status == LpicModel::STATUS_ACTIVE)) {

                } else {
                    $cond_item['item_id'] = $data['item_id'];
                    $max_priority = M("live_pic_pic")->where($cond_item)->max('priority');
                    $data['priority'] = $max_priority + 1;
                }
            } else {
                $data['priority'] = 0;
            }
            M("live_pic_pic")->where($cond_pic)->save($data);
        }
        $this->ajaxReturn(['code' => 1, 'msg' => '成功', 'live_id' => $data['live_id']]);
    }

    public function picDelete()
    {
        $picId = I("get.picId", "", "");
        $data['status'] = LpicModel::STATUS_DELETE;
        if (!empty($picId)) {
            M("live_pic_pic")->where(['pic_id' => $picId])->save($data);
        }
        return $this->my_success("成功");
    }
}

?>
